package com.fenye.testflow.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fenye.testflow.model.common.Result;
import com.fenye.testflow.model.common.ResultCodeEnum;
import com.fenye.testflow.model.entity.testflow.Version;
import com.fenye.testflow.model.entity.testflow.Version;
import com.fenye.testflow.service.VersionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/version")
public class VersionContoller {

    @Autowired
    private VersionService versionService;

    /**
     *  @author: fenye
     *  @Date:
     *  @Description: 新增
     */
    @PostMapping("save")
    public Result save(@RequestBody Version version) {
        boolean isSave = versionService.save(version);
        if (isSave) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @PostMapping("update")
    public Result update(@RequestBody Version version) {
        boolean isUpdate = versionService.updateById(version);
        if (isUpdate) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @DeleteMapping("/delete/{versionId}")
    public Result delete(@PathVariable Long versionId) {
        boolean isDelete = versionService.removeById(versionId);
        if (isDelete) {
            return Result.build(null, ResultCodeEnum.SUCCESS);
        }
        return Result.build(null,ResultCodeEnum.DATA_ERROR);
    }

    @GetMapping("/findById/{versionId}")
    public Result findByid(@RequestParam Long versionId) {
        Version version = versionService.getById(versionId);
        if (version == null) {
            return Result.build(null,ResultCodeEnum.DATA_ERROR);
        }
        return Result.build(version, ResultCodeEnum.SUCCESS);
    }

    @GetMapping("findByPage/{page}/{size}")
    public Result findByPage(Version version,
            @PathVariable int page,
            @PathVariable int size) {

        Page<Version> pageParam = new Page<>(page, size);
        Page<Version> pageData = versionService.page(pageParam);
        return Result.build(pageData, ResultCodeEnum.SUCCESS);
    }

}
